package com.ailesson.video.service.impl;

import com.ailesson.common.except.GlobalErrorCodeConstants;
import com.ailesson.common.except.ServiceException;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;

import com.ailesson.video.mapper.CourseCollectMapper;
import com.ailesson.video.entity.CourseCollectEntity;
import com.ailesson.video.service.CourseCollectService;
import com.ailesson.video.model.req.CourseCollectQueryReq;
import com.ailesson.video.model.req.CourseCollectReq;
import com.ailesson.video.model.reply.CourseCollectReply;
import com.ailesson.common.reply.PageResult;
import cn.hutool.core.collection.CollectionUtil;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class CourseCollectServiceImpl extends ServiceImpl<CourseCollectMapper, CourseCollectEntity> implements CourseCollectService {

    @Override
    public PageResult queryPage(CourseCollectQueryReq params) {
       Page pageTmp = Page.of(params.getPageNo(),params.getPageSize());
       IPage<CourseCollectEntity> page = this.page(
               pageTmp,
               new LambdaQueryWrapper<CourseCollectEntity>().orderByDesc(CourseCollectEntity::getId)
       );
       List<CourseCollectReply> replyList = Collections.EMPTY_LIST;
       if (CollectionUtil.isNotEmpty(page.getRecords())){
           replyList = page.getRecords().stream().map(x -> {
               CourseCollectReply reply = new CourseCollectReply();
               BeanUtils.copyProperties(x, reply);
               return reply;
           }).collect(Collectors.toList());
       }
       return new PageResult<>(replyList,page.getTotal());
    }

      @Override
        public CourseCollectReply info(Long id) {
            CourseCollectEntity entity = this.getById(id);
            if (entity != null){
                CourseCollectReply reply = new CourseCollectReply();
                BeanUtils.copyProperties(entity, reply);
                return reply;
            }
            return null;
        }

        @Transactional(
                rollbackFor = {Exception.class}
        )
        @Override
        public void update(CourseCollectReq courseCollect) {
            if (courseCollect.getId() == null || courseCollect.getId() <= 0){
                throw new ServiceException(GlobalErrorCodeConstants.ID_EMPTY_ERROR);
            }
            CourseCollectEntity entity = this.getById(courseCollect.getId());
            if (entity == null){
                throw new ServiceException(GlobalErrorCodeConstants.NOT_EXISTS_ERROR);
            }
            BeanUtils.copyProperties(courseCollect, entity);
            this.updateById(entity);
        }

        @Transactional(
                rollbackFor = {Exception.class}
        )
        @Override
        public Long add(CourseCollectReq courseCollect) {
            courseCollect.setId(null);
            CourseCollectEntity entity = new CourseCollectEntity();
            BeanUtils.copyProperties(courseCollect, entity);
            this.save(entity);
            return entity.getId();
        }

}